package org.dromara.hm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.hm.domain.bo.HmPickupRequestBo;
import org.dromara.hm.domain.bo.HmSaleOrderPickUpBo;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderPickUpVo;
import org.dromara.hm.service.IHmSaleOrderPickUpService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 销售订单提货、回款
 *
 * @author Hzy
 * @date 2025-01-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/saleOrderPickUp")
public class HmSaleOrderPickUpController extends BaseController
{

    private final IHmSaleOrderPickUpService hmSaleOrderPickUpService;

    /**
     * 查询销售订单提货、回款列表
     */
    @SaCheckPermission("hm:saleOrderPickUp:list")
    @GetMapping("/list")
    public TableDataInfo<HmSaleOrderPickUpVo> list(HmSaleOrderPickUpBo bo, PageQuery pageQuery)
    {
        return hmSaleOrderPickUpService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售订单提货、回款列表
     */
    @SaCheckPermission("hm:saleOrderPickUp:export")
    @Log(title = "销售订单提货、回款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmSaleOrderPickUpBo bo, HttpServletResponse response)
    {
        List<HmSaleOrderPickUpVo> list = hmSaleOrderPickUpService.queryList(bo);
        ExcelUtil.exportExcel(list, "销售订单提货、回款", HmSaleOrderPickUpVo.class, response);
    }

    /**
     * 获取销售订单提货、回款详细信息
     *
     * @param pickUpId 主键
     */
    @SaCheckPermission("hm:saleOrderPickUp:query")
    @GetMapping("/{pickUpId}")
    public R<HmSaleOrderPickUpVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long pickUpId)
    {
        return R.ok(hmSaleOrderPickUpService.queryById(pickUpId));
    }

    /**
     * 新增销售订单提货 (支持多项提货和补充提货)
     */
    @SaCheckPermission("hm:saleOrderPickUp:add")
    @Log(title = "新增销售订单提货", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/pickUp")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmPickupRequestBo requestBo) // 接收新的请求BO
    {
        return toAjax(hmSaleOrderPickUpService.insertByPickupRequest(requestBo)); // 调用新的Service方法
    }

    /**
     * 新增销售订单回款
     */
    @SaCheckPermission("hm:saleOrderPickUp:add")
    @Log(title = "新增销售订单回款", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/receipt")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmSaleOrderPickUpBo bo) // 接收新的请求BO
    {
        return toAjax(hmSaleOrderPickUpService.handlePaymentLogic(bo)); // 调用新的Service方法
    }

    /**
     * 修改销售订单提货、回款[作废提货、回款]
     */
    @SaCheckPermission("hm:saleOrderPickUp:edit")
    @Log(title = "销售订单提货、回款", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmSaleOrderPickUpBo bo)
    {
        return toAjax(hmSaleOrderPickUpService.updateByBo(bo));
    }

    /**
     * 删除销售订单提货、回款
     *
     * @param pickUpIds 主键串
     */
    @SaCheckPermission("hm:saleOrderPickUp:remove")
    @Log(title = "销售订单提货、回款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pickUpIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] pickUpIds)
    {
        return toAjax(hmSaleOrderPickUpService.deleteWithValidByIds(List.of(pickUpIds), true));
    }
}
